package com.jn.erp.web.api.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.web.api.student.entity.Student;
import com.jn.erp.web.api.student.entity.StudentContactRecord;
import com.jn.erp.web.api.student.mapper.StudentContactRecordMapper;
import com.jn.erp.web.api.student.mapper.StudentMapper;
import com.jn.erp.web.api.student.service.StudentContactRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.jn.erp.security.utils.SecurityUtils.getUserId;

@Service
public class StudentContactRecordServiceImpl extends ServiceImpl<StudentContactRecordMapper, StudentContactRecord> implements StudentContactRecordService {

    @Autowired
    private StudentContactRecordMapper studentContactRecordMapper;

    @Autowired
    private StudentContactRecordService studentContactRecordService;

    @Autowired
    private  StudentMapper studentMapper;

    @Override
    public boolean saveOrUpdateByRecord(StudentContactRecord studentContactRecord) {
        // 新增
      if ( studentContactRecord.getType().equals("1")) {
            StudentContactRecord student = new StudentContactRecord();
          student.setContactTime(studentContactRecord.getContactTime());
          student.setState(studentContactRecord.getState());
//          跟进阶段
//          student.setStates(studentContactRecord.getStates());
          student.setPhase(studentContactRecord.getPhase());
          student.setInfo(studentContactRecord.getInfo());
          student.setStudentId(studentContactRecord.getStudentId());
          student.setOrgId(studentContactRecord.getOrgId());
          long studentId = studentContactRecord.getStudentId();
          // 新增一条跟进记录 当前登录人为跟进人
          Student student1 = studentMapper.selectById(studentId);
          student1.setCounselorId(getUserId());
          studentMapper.updateById(student1);
          return this.save(student);

            // 修改
        } else if (studentContactRecord.getType().equals("2")){
          StudentContactRecord stu = studentContactRecordMapper.selectById(studentContactRecord.getId());
          // 修改跟进时间
          stu.setContactTime(studentContactRecord.getContactTime());
          stu.setState(studentContactRecord.getState());
          stu.setStates(studentContactRecord.getStates());
          stu.setPhase(studentContactRecord.getPhase());
          stu.setInfo(studentContactRecord.getInfo());
          stu.setStudentId(studentContactRecord.getStudentId());
          return this.updateById(stu);
      }
        return true;
    }

    @Override
    public List<StudentContactRecord> followList(StudentContactRecord studentContactRecord) {
        //跟进人id
        studentContactRecord.setCounId(String.valueOf(getUserId()));
        List<StudentContactRecord> stu = studentContactRecordMapper.followList(studentContactRecord);
        return stu;
    }

    @Override
    public List<StudentContactRecord> clueList(StudentContactRecord studentContactRecord) {
        List<StudentContactRecord> clue = studentContactRecordMapper.clueList(studentContactRecord);
        return clue;
    }

    @Override
    public List<StudentContactRecord> followListById( StudentContactRecord studentContactRecord) {
        List<StudentContactRecord> stu = studentContactRecordMapper.followListById(studentContactRecord);
        return stu;
    }
}
  